﻿using System.Windows;
using System.Windows.Media;

namespace SegmentDisplay.Common.Digit07
{
    /// <summary>
    /// Control SegmentDigit07
    /// </summary>
    public class SegmentDigit07 : SegmentBase
    {

        #region Input values

        /// <summary>
        /// Input value 
        /// </summary>
        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the Value.
        /// </summary>
        public static DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(SegmentDigit07), new PropertyMetadata("Empty", new PropertyChangedCallback(ValueChanged)));

        /// <summary>
        /// Function on change Value property 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SegmentDigit07 sh = (SegmentDigit07)d;
            var _currentValue = "Empty";
            if (e.NewValue != null)
            {
                if (e.NewValue is string)
                {
                    string digit = (string)e.NewValue;
                    _currentValue = digit.Substring(0, 1);
                }
                else if (e.NewValue is int)
                {
                    int digit = (int)e.NewValue;
                    _currentValue = digit.ToString("0");
                }
                sh.UpdateSegments(sh.IsSwitchOn, _currentValue);
            }
        }

        /// <summary>
        /// Changing on change a value 
        /// </summary>
        /// <param name="isSwitchOn"></param>
        /// <param name="newValue"></param>
        protected void UpdateSegments(bool isSwitchOn, string newValue)
        {
            if (isSwitchOn)
                SetDigit(newValue);
            else
                SetDigit("Empty");
        }

        /// <summary>
        /// Default update with current values
        /// </summary>
        protected void UpdateSegments()
        {
            UpdateSegments(IsSwitchOn, Value);
        }

        /// <summary>
        /// Set values of segments according parameter
        /// </summary>
        /// <param name="vector"></param>
        private void SetSegmentVector(Values07 value)
        {
            SegmentA = GetBrush(value.HasFlag(Values07.SegmentA));
            SegmentB = GetBrush(value.HasFlag(Values07.SegmentB));
            SegmentC = GetBrush(value.HasFlag(Values07.SegmentC));
            SegmentD = GetBrush(value.HasFlag(Values07.SegmentD));
            SegmentE = GetBrush(value.HasFlag(Values07.SegmentE));
            SegmentF = GetBrush(value.HasFlag(Values07.SegmentF));
            SegmentG = GetBrush(value.HasFlag(Values07.SegmentG));
        }

        /// <summary>
        /// public function for force change the value
        /// </summary>
        /// <param name="digit"></param>
        public void SetDigit(string digit)
        {
            SetSegmentVector(ValueSet07.GetVector(digit));
        }

        #endregion

        #region Management of serment

        /// <summary>
        /// Override a behavior on changing the IsSwitchOn value
        /// </summary>
        /// <param name="newValue"></param>
        public override void OnIsSwitchOnChanged(bool newValue)
        {
            base.OnIsSwitchOnChanged(newValue);
            UpdateSegments(newValue, Value);
        }

        #endregion

        #region Segments

        /// <summary>
        /// Gets or sets the SegmentA to render.
        /// </summary>
        public Brush SegmentA
        {
            get { return (Brush)GetValue(SegmentAProperty); }
            set { SetValue(SegmentAProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentA to render.
        /// </summary>
        public static DependencyProperty SegmentAProperty = 
            DependencyProperty.Register("SegmentA", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentB to render.
        /// </summary>
        public Brush SegmentB
        {
            get { return (Brush)GetValue(SegmentBProperty); }
            set { SetValue(SegmentBProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentB to render.
        /// </summary>
        public static DependencyProperty SegmentBProperty = 
            DependencyProperty.Register("SegmentB", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentC to render.
        /// </summary>
        public Brush SegmentC
        {
            get { return (Brush)GetValue(SegmentCProperty); }
            set { SetValue(SegmentCProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentC to render.
        /// </summary>
        public static DependencyProperty SegmentCProperty = 
            DependencyProperty.Register("SegmentC", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentD to render.
        /// </summary>
        public Brush SegmentD
        {
            get { return (Brush)GetValue(SegmentDProperty); }
            set { SetValue(SegmentDProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentD to render.
        /// </summary>
        public static DependencyProperty SegmentDProperty = 
            DependencyProperty.Register("SegmentD", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentE to render.
        /// </summary>
        public Brush SegmentE
        {
            get { return (Brush)GetValue(SegmentEProperty); }
            set { SetValue(SegmentEProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentE to render.
        /// </summary>
        public static DependencyProperty SegmentEProperty =
            DependencyProperty.Register("SegmentE", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentF to render.
        /// </summary>
        public Brush SegmentF
        {
            get { return (Brush)GetValue(SegmentFProperty); }
            set { SetValue(SegmentFProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentF to render.
        /// </summary>
        public static DependencyProperty SegmentFProperty =
            DependencyProperty.Register("SegmentF", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        /// <summary>
        /// Gets or sets the SegmentG to render.
        /// </summary>
        public Brush SegmentG
        {
            get { return (Brush)GetValue(SegmentGProperty); }
            set { SetValue(SegmentGProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the SegmentG to render.
        /// </summary>
        public static DependencyProperty SegmentGProperty = 
            DependencyProperty.Register("SegmentG", typeof(Brush), typeof(SegmentDigit07), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        #endregion

        #region Constructor 

        /// <summary>
        /// Static constructor
        /// </summary>
        static SegmentDigit07()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SegmentDigit07), new FrameworkPropertyMetadata(typeof(SegmentDigit07)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public SegmentDigit07()
        {
            DefaultStyleKey = typeof(SegmentDigit07);
        }

        #endregion
    }
}
